package dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import dao.BaseDao;
import dao.EasyBuyNewsDao;
import entity.Easybuy_news;
import entity.Easybuy_order_detail;
import utils.PageUtils;

public class EasyBuyEewsDaoImpl extends BaseDao implements EasyBuyNewsDao{

	
	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	
	
	@Override
	public Easybuy_news Information(String title) {
		// TODO Auto-generated method stub
		Easybuy_news news=null;
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_news where title=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, title);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				news=new Easybuy_news();
				news.setId(rs.getInt("id"));
				news.setContent(rs.getString("content"));
				news.setCreateTime(rs.getString("createTime"));
				news.setTitle(rs.getString("title"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return news;
	}


	@Override
	public List<Easybuy_news> InformationList(int count) {
		// TODO Auto-generated method stub
		List<Easybuy_news> list=new ArrayList<Easybuy_news>();
		int page=(count-1)*8;
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_news limit ?,8";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, page);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				Easybuy_news news=new Easybuy_news();
				news.setId(rs.getInt("id"));
				news.setContent(rs.getString("content"));
				news.setCreateTime(rs.getString("createTime"));
				news.setTitle(rs.getString("title"));
				list.add(news);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}


	@Override
	public int count() {
		// TODO Auto-generated method stub
		int result=0;
		try {
			conn=super.getConnection();
			String sql="select count(1) from easybuy_news";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				result=rs.getInt(1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		
		return result;
	}


	@Override
	public List<Easybuy_news> list() {
		// TODO Auto-generated method stub
		List<Easybuy_news> list=new ArrayList<Easybuy_news>();
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_news order by createTime desc limit 0,5";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				Easybuy_news news=new Easybuy_news();
				news.setId(rs.getInt("id"));
				news.setContent(rs.getString("content"));
				news.setCreateTime(rs.getString("createTime"));
				news.setTitle(rs.getString("title"));
				list.add(news);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}


	@Override
	public PageUtils<Easybuy_news> newsList(int pageindex, int pageSize) {
		List<Easybuy_news> list=new ArrayList<Easybuy_news>();
		PageUtils<Easybuy_news> utils=new PageUtils<>();
		utils.setPagesize(pageSize);
		try {
    		
    		conn = super.getConnection();
    		
    		String sql=" select * from easybuy_news limit ?,?";
    		
    		pstmt = conn.prepareStatement(sql);
    		pstmt.setInt(1, pageindex);
			pstmt.setInt(2, pageSize);
			
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Easybuy_news news=new Easybuy_news();
				news.setContent(rs.getString("content"));
				news.setCreateTime(rs.getString("createTime"));
				news.setId(rs.getInt("id"));
				news.setTitle(rs.getString("title"));
				list.add(news);
			}
			
			utils.setList(list);
			int total=0;
			String sqlCount="select count(1) from easybuy_news";
			
			pstmt=conn.prepareStatement(sqlCount);
			rs=pstmt.executeQuery();
			if (rs.next()) {
				total=rs.getInt(1);
			}
			
			utils.setPagetotal(total);
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	super.closeAll(conn, pstmt, rs);
        }
		return utils;
	}
	
	

}
